﻿<div class="page">
    <div id="header" class="bar">
        <div id="btnBack" class="btn-left">
            <span>Back</span>
        </div>
    </div>
    <div id="content">
        <div class="group">
            <textarea id="txtContent"></textarea>
        </div>
        <div class="group">
            <div id="btnSave" class="btn">
                <span>Save And Go Back</span>
            </div>
        </div>
    </div>
</div>
<script language="javascript" type="text/javascript">
    (function () {
        var editingBlog = null;
        var service = new DbService();
        var timer = null;
        var backToListPage = true;

        function save(callback) {
            editingBlog.content = $("#txtContent").val();
            service.saveBlog(editingBlog, function () {
                if (callback != undefined && callback != null) {
                    callback();
                }
            });
        }

        function goback() {
            clearInterval(timer);
            if (backToListPage) {
                nova.application.goBack("home.html");
            } else {
                nova.application.goBack();
            }
        }

        $(document).ready(function () {
            editingBlog = nova.application.currentPage.blog;
            if (nova.application.currentPage.backToListPage != undefined) {
                backToListPage = nova.application.currentPage.backToListPage;
            }
            $("#txtContent").val(editingBlog.content);
            nova.touch.bindClick("#btnBack", function () {
                goback();
            });
            nova.touch.bindClick("#btnSave", function () {
                save(function () {
                    goback();
                });
            });
            nova.application.currentPage.backbutton(function () {
                goback();
            });
            service.getInterval(function (interval) {
                timer = setInterval(function () {
                    save(function () {
                        var time = new Date();
                        var message = "saved at " + time.getHours() + ":" + time.getMinutes() + ":" + time.getSeconds();
                        new nova.widgets.Toast(message).show();
                    });
                }, interval * 1000);
            });
        });
    })();
</script>